import { createAsyncThunk } from '@reduxjs/toolkit';
import axios, { AxiosError } from 'axios';
import { API } from '../../helpers/API';
import { RootState } from '..';

export const fetchAddFavoritesSlice = createAsyncThunk<_, _, {state: RootState}>(
	'favorites/fetchAddFavoritesSlice',
	async (favoriteBall, thunkAPI) => {
		try {
			const token = thunkAPI.getState().authorization.user.token;
			const { data } = await axios.post(`${API}/auth_me`,favoriteBall, {
				headers: { 
					Authorization: `Bearer ${token}`	
				}
			});
			console.log('данные с бека по добавленным в избранные', data);
			return data;
		} catch (error) {
			if (error instanceof AxiosError) {
				throw new Error(error.response?.data.message);
			}
		}
	}
);